3 use Wikimedia\ScopedCallback
;
6 * @author Matthias Mullie <mmullie@wikimedia.org>
9 class BagOStuffTest
extends MediaWikiTestCase
{
13 const TEST_KEY
= 'test';
15 protected function setUp() {
18 // type defined through parameter
19 if ( $this->getCliArg( 'use-bagostuff' ) ) {
20 $name = $this->getCliArg( 'use-bagostuff' );
22 $this->cache
= ObjectCache
::newFromId( $name );
24 // no type defined - use simple hash
25 $this->cache
= new HashBagOStuff
;
28 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
29 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
33 * @covers BagOStuff::makeGlobalKey
34 * @covers BagOStuff::makeKeyInternal
36 public function testMakeKey() {
37 $cache = ObjectCache
::newFromId( 'hash' );
39 $localKey = $cache->makeKey( 'first', 'second', 'third' );
40 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
42 $this->assertStringMatchesFormat(
43 '%Sfirst%Ssecond%Sthird%S',
45 'Local key interpolates parameters'
48 $this->assertStringMatchesFormat(
49 'global%Sfirst%Ssecond%Sthird%S',
51 'Global key interpolates parameters and contains global prefix'
54 $this->assertNotEquals(
57 'Local key and global key with same parameters should not be equal'
60 $this->assertNotEquals(
61 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
62 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
67 * @covers BagOStuff::merge
68 * @covers BagOStuff::mergeViaCas
70 public function testMerge() {
71 $key = $this->cache
->makeKey( self
::TEST_KEY
);
74 $casRace = false; // emulate a race
75 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
79 $cache->set( $key, 'conflict', 5 );
82 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
85 // merge on non-existing value
86 $merged = $this->cache
->merge( $key, $callback, 5 );
87 $this->assertTrue( $merged );
88 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
90 // merge on existing value
91 $merged = $this->cache
->merge( $key, $callback, 5 );
92 $this->assertTrue( $merged );
93 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
98 $this->cache
->merge( $key, $callback, 5, 1 ),
99 'Non-blocking merge (CAS)'
101 $this->assertEquals( 1, $calls );
105 * @covers BagOStuff::merge
106 * @dataProvider provideTestMerge_fork
108 public function testMerge_fork( $exists, $childWins, $resCAS ) {
109 $key = $this->cache
->makeKey( self
::TEST_KEY
);
110 $pCallback = function ( BagOStuff
$cache, $key, $oldVal ) {
111 return ( $oldVal === false ) ?
'init-parent' : $oldVal . '-merged-parent';
113 $cCallback = function ( BagOStuff
$cache, $key, $oldVal ) {
114 return ( $oldVal === false ) ?
'init-child' : $oldVal . '-merged-child';
118 $this->cache
->set( $key, 'x', 5 );
122 * Test concurrent merges by forking this process, if:
123 * - not manually called with --use-bagostuff
124 * - pcntl_fork is supported by the system
125 * - cache type will correctly support calls over forks
127 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
128 $fork &= function_exists( 'pcntl_fork' );
129 $fork &= !$this->cache
instanceof HashBagOStuff
;
130 $fork &= !$this->cache
instanceof EmptyBagOStuff
;
131 $fork &= !$this->cache
instanceof MultiWriteBagOStuff
;
134 // Function to start merge(), run another merge() midway through, then finish
135 $func = function ( $cache, $key, $cur ) use ( $pCallback, $cCallback, &$pid ) {
140 pcntl_wait( $status );
142 return $pCallback( $cache, $key, $cur );
144 $this->cache
->merge( $key, $cCallback, 0, 1 );
145 // Bail out of the outer merge() in the child process since it does not
146 // need to attempt to write anything. Success is checked by the parent.
147 parent
::tearDown(); // avoid phpunit notices
152 // attempt a merge - this should fail
153 $merged = $this->cache
->merge( $key, $func, 0, 1 );
156 return; // can't fork, ignore this test...
159 // merge has failed because child process was merging (and we only attempted once)
160 $this->assertEquals( !$childWins, $merged );
161 $this->assertEquals( $this->cache
->get( $key ), $resCAS );
163 $this->markTestSkipped( 'No pcntl methods available' );
167 function provideTestMerge_fork() {
169 // (already exists, child wins CAS, result of CAS)
170 [ false, true, 'init-child' ],
171 [ true, true, 'x-merged-child' ]
176 * @covers BagOStuff::changeTTL
178 public function testChangeTTL() {
179 $key = $this->cache
->makeKey( self
::TEST_KEY
);
182 $this->cache
->add( $key, $value, 5 );
183 $this->assertTrue( $this->cache
->changeTTL( $key, 5 ) );
184 $this->assertEquals( $this->cache
->get( $key ), $value );
185 $this->cache
->delete( $key );
186 $this->assertFalse( $this->cache
->changeTTL( $key, 5 ) );
190 * @covers BagOStuff::add
192 public function testAdd() {
193 $key = $this->cache
->makeKey( self
::TEST_KEY
);
194 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
198 * @covers BagOStuff::get
200 public function testGet() {
201 $value = [ 'this' => 'is', 'a' => 'test' ];
203 $key = $this->cache
->makeKey( self
::TEST_KEY
);
204 $this->cache
->add( $key, $value, 5 );
205 $this->assertEquals( $this->cache
->get( $key ), $value );
209 * @covers BagOStuff::get
210 * @covers BagOStuff::set
211 * @covers BagOStuff::getWithSetCallback
213 public function testGetWithSetCallback() {
214 $key = $this->cache
->makeKey( self
::TEST_KEY
);
215 $value = $this->cache
->getWithSetCallback(
219 return 'hello kitty';
223 $this->assertEquals( 'hello kitty', $value );
224 $this->assertEquals( $value, $this->cache
->get( $key ) );
228 * @covers BagOStuff::incr
230 public function testIncr() {
231 $key = $this->cache
->makeKey( self
::TEST_KEY
);
232 $this->cache
->add( $key, 0, 5 );
233 $this->cache
->incr( $key );
235 $actualValue = $this->cache
->get( $key );
236 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
240 * @covers BagOStuff::incrWithInit
242 public function testIncrWithInit() {
243 $key = $this->cache
->makeKey( self
::TEST_KEY
);
244 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
245 $this->assertEquals( 3, $val, "Correct init value" );
247 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
248 $this->assertEquals( 4, $val, "Correct init value" );
252 * @covers BagOStuff::getMulti
254 public function testGetMulti() {
255 $value1 = [ 'this' => 'is', 'a' => 'test' ];
256 $value2 = [ 'this' => 'is', 'another' => 'test' ];
257 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
258 $value4 = [ 'another test where chars in key will be encoded' ];
260 $key1 = $this->cache
->makeKey( 'test-1' );
261 $key2 = $this->cache
->makeKey( 'test-2' );
262 // internally, MemcachedBagOStuffs will encode to will-%25-encode
263 $key3 = $this->cache
->makeKey( 'will-%-encode' );
264 $key4 = $this->cache
->makeKey(
265 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
269 $this->cache
->delete( $key1 );
270 $this->cache
->delete( $key2 );
271 $this->cache
->delete( $key3 );
272 $this->cache
->delete( $key4 );
274 $this->cache
->add( $key1, $value1, 5 );
275 $this->cache
->add( $key2, $value2, 5 );
276 $this->cache
->add( $key3, $value3, 5 );
277 $this->cache
->add( $key4, $value4, 5 );
280 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
281 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
285 $this->cache
->delete( $key1 );
286 $this->cache
->delete( $key2 );
287 $this->cache
->delete( $key3 );
288 $this->cache
->delete( $key4 );
292 * @covers BagOStuff::setMulti
293 * @covers BagOStuff::deleteMulti
295 public function testSetDeleteMulti() {
297 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
298 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
299 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
300 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
301 $this->cache
->makeKey( 'test-5' ) => 4,
302 $this->cache
->makeKey( 'test-6' ) => 'ever'
305 $this->cache
->setMulti( $map, 5 );
308 $this->cache
->getMulti( array_keys( $map ) )
311 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ), 5 ) );
315 $this->cache
->getMulti( array_keys( $map ) )
320 * @covers BagOStuff::getScopedLock
322 public function testGetScopedLock() {
323 $key = $this->cache
->makeKey( self
::TEST_KEY
);
324 $value1 = $this->cache
->getScopedLock( $key, 0 );
325 $value2 = $this->cache
->getScopedLock( $key, 0 );
327 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
328 $this->assertNull( $value2, 'Duplicate call returned no lock' );
332 $value3 = $this->cache
->getScopedLock( $key, 0 );
333 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
336 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
337 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
339 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
340 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
344 * @covers BagOStuff::__construct
345 * @covers BagOStuff::trackDuplicateKeys
347 public function testReportDupes() {
348 $logger = $this->createMock( Psr\Log\NullLogger
::class );
349 $logger->expects( $this->once() )
350 ->method( 'warning' )
351 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
356 $cache = new HashBagOStuff( [
357 'reportDupes' => true,
358 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
361 $cache->get( 'foo' );
362 $cache->get( 'bar' );
363 $cache->get( 'foo' );
365 DeferredUpdates
::doUpdates();
369 * @covers BagOStuff::lock()
370 * @covers BagOStuff::unlock()
372 public function testLocking() {
374 $this->assertTrue( $this->cache
->lock( $key ) );
375 $this->assertFalse( $this->cache
->lock( $key ) );
376 $this->assertTrue( $this->cache
->unlock( $key ) );
379 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
380 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
381 $this->assertTrue( $this->cache
->unlock( $key2 ) );
382 $this->assertTrue( $this->cache
->unlock( $key2 ) );